package com.example.demo.controller;

import com.example.demo.entity.ParkingLot;
import com.example.demo.entity.PricingStandard;
import com.example.demo.entity.Vehicle;
import com.example.demo.service.ParkingLotService;
import com.example.demo.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
//自动识别跳转路径
@RequestMapping("/vehicle")
public class VehicleController {

    @Value("${save.path}")
    String savePath;//图片存储路径
    private final VehicleService service;
    private final ParkingLotService parkingLotService;
    @Autowired
    public VehicleController(VehicleService service,ParkingLotService parkingLotService){
        this.service=service;
        this.parkingLotService=parkingLotService;
    }

    @RequestMapping("/list")//和HTML文件对标
    public String list(Model model){
        //通过model传值到页面
        List<Vehicle> entities=service.getAll();
        model.addAttribute("list",entities);//model打包成list属性完成List对象的值传递
        return "/vehicle/list";
    }


    @RequestMapping(value = "/insert")
    public String insert(Model model){
        // 新建user对象,给到ui的model
        model.addAttribute("vehicle",new Vehicle());
        List<ParkingLot> parkingLots=parkingLotService.getAll();
        model.addAttribute("parkingLots",parkingLots);
        return "/vehicle/edit";
    }


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(@ModelAttribute("vehicle") Vehicle entity
            , @RequestParam("isActive") boolean isActive
            , @RequestParam("parkingLotId") Long parkingLotId
            , @RequestParam("file") MultipartFile file) throws Exception {

        entity.setActive(isActive);
        ParkingLot parkingLot=parkingLotService.getById(parkingLotId);
        entity.setParkingLot(parkingLot);

        // 上传图片文件到指定目录
        String fileName=System.currentTimeMillis()+"-"+file.getOriginalFilename();//生成文件名
        Path saveTo= Paths.get(savePath,fileName);//设置文件存储的路径
        Files.write(saveTo,file.getBytes());//存储文件

        // 存储图片URL
        entity.setPiUrl("/images/"+fileName);

        boolean result=entity.getId()!=null?service.update(entity):service.insert(entity);

        return "redirect:/vehicle/list";
    }


    @RequestMapping(value = "/edit/{id}")
    public String entity(@PathVariable("id") Long id, Model model){
        Vehicle entity=service.getById(id);
        model.addAttribute("vehicle",entity);
        List<ParkingLot> parkingLots=parkingLotService.getAll();
        model.addAttribute("parkingLots",parkingLots);
        return "/vehicle/edit";
    }

    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id")Long id,Model model){
        Vehicle entity=service.getById(id);
        model.addAttribute("vehicle",entity);
        return "/vehicle/detail";
    }


    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        boolean entity=service.delete(id);
        return "redirect:/vehicle/list";
    }



}
